package com.myhome.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.myhome.config.constant.R;
import com.myhome.domain.Market;
import com.myhome.domain.TurnoverOrder;
import com.myhome.service.MarketService;
import com.myhome.service.TurnoverOrderService;
import com.myhome.vo.DepthItemVo;
import com.myhome.vo.DepthsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
 * @Author QYiXin
 * @Date 2021 11 03 22 21
 **/
@RestController
@Api(tags = "市场控制器")
@RequestMapping("/markets")
public class MarketController {
    @Autowired
    private MarketService marketService;

    @Autowired
    private TurnoverOrderService turnoverOrderService;



    @GetMapping
    @ApiOperation(value = "交易市场的分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('trade_market_query')")
    public R<Page<Market>> findByPage(@ApiIgnore Page<Market> page, Long tradeAreaId, Byte status) {
        Page<Market> pageData = marketService.findByPage(page, tradeAreaId, status);
        return R.ok(pageData);
    }


    @PostMapping("/setStatus")
    @ApiOperation(value = "启用/禁用交易市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "market的json数据")
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R setStatus(@RequestBody Market market) {
        boolean updateById = marketService.updateById(market);
        if (updateById) {
            return R.ok();
        }
        return R.fail("状态设置失败");
    }


    @PostMapping
    @ApiOperation(value = "新增一个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "marketjson")
    })
    @PreAuthorize("hasAuthority('trade_market_create')")
    public R save(@RequestBody Market market) {
        boolean save = marketService.save(market);
        if (save) {
            return R.ok();
        }
        return R.fail("新增失败");
    }


    @PatchMapping
    @ApiOperation(value = "修改一个市场")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "market", value = "marketjson")
    })
    @PreAuthorize("hasAuthority('trade_market_update')")
    public R update(@RequestBody Market market) {
        boolean updateById = marketService.updateById(market);
        if (updateById) {
            return R.ok();
        }
        return R.fail("修改失败");
    }


    @GetMapping("/all")
    @ApiOperation(value = "查询所有的交易市场")
    public R<List<Market>> listMarks() {
        return R.ok(marketService.list());
    }



    @ApiOperation(value = "查询成交记录")
    @GetMapping("/trades/{symbol}")
    public R<List<TurnoverOrder>> findSymbolTurnoverOrder(@PathVariable("symbol") String symbol) {
        List<TurnoverOrder> turnoverOrders = turnoverOrderService.findBySymbol(symbol);
        return R.ok(turnoverOrders);
    }

    @GetMapping("/depth/{symbol}/{depth}")
    public R<DepthsVo> getDepthVo(@PathVariable("symbol") String symbol, @PathVariable("depth") String depth) {
        Market market = marketService.getMarkerBySymbol(symbol);
        if (market == null) {
            throw new IllegalArgumentException("该市场不存在");
        }
        DepthsVo depthsVo = new DepthsVo();
        depthsVo.setBids(Arrays.asList(new DepthItemVo(BigDecimal.valueOf(7.00000), BigDecimal.valueOf(100)), new DepthItemVo(BigDecimal.valueOf(5.99999), BigDecimal.valueOf(200))));
        depthsVo.setAsks(Arrays.asList(new DepthItemVo(BigDecimal.valueOf(7.00000), BigDecimal.valueOf(100)), new DepthItemVo(BigDecimal.valueOf(5.99999), BigDecimal.valueOf(200))));
        depthsVo.setPrice(market.getOpenPrice());
        depthsVo.setCnyPrice(market.getOpenPrice());
        return R.ok(depthsVo);
    }
}
